package com.me.mygdxgame.util;

import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.MathUtils;
import com.badlogic.gdx.math.Vector2;
import com.me.mygdxgame.models.AbstractGameObject;
import com.me.mygdxgame.view.WorldRenderer;

public class CameraHelper {
	
	public static final String TAG = CameraHelper.class.getName();
	
	private final float MAX_ZOOM_IN = 0.25f;
	private final float MAX_ZOOM_OUT = 10.0f;
	
	private Vector2 position;
	private float zoom;
	private AbstractGameObject target;

	public CameraHelper(){
		position = new Vector2();
//		position.set(WorldRenderer.positionInitCamera.x, WorldRenderer.positionInitCamera.y);
		zoom = 1;
	}
	
	public void update(float deltaTime){
		if(!hasTarget())return;
		
		position.x = target.sprite.getX() + target.sprite.getOriginX();
		position.y = target.sprite.getY() + target.sprite.getOriginY();
	}

	public boolean hasTarget() {
		// TODO Auto-generated method stub
		return target != null;
	}
	
	public boolean hasTarget(AbstractGameObject target){
		return hasTarget() && this.target.equals(target);	
	}

	public AbstractGameObject getTarget() {
		return target;
	}

	public void setTarget(AbstractGameObject target) {
		this.target = target;
	}
	
	

	public void applyTo(OrthographicCamera camera) {
		if(position.x < WorldRenderer.positionInitCamera.x)
			position.x = WorldRenderer.positionInitCamera.x;
		else if(position.x > 12000 - WorldRenderer.positionInitCamera.x - 800)
			position.x = 12000 - WorldRenderer.positionInitCamera.x - 800;
			
		if(position.y < WorldRenderer.positionInitCamera.y)
			position.y = WorldRenderer.positionInitCamera.y;
		else if(position.y > 720/2)
			position.y = 720/2;
		camera.position.x = position.x;
		camera.position.y = position.y;
		camera.zoom = zoom;
		camera.update();
	}

	public Vector2 getPosition() {
		return position;
	}

	public void setPosition(float x, float y) {
		this.position.set(x, y);
	}
	
	public void addZoom(float amount){setZoom(zoom + amount);}
	
	public void setZoom(float zoom){
		this.zoom = MathUtils.clamp(zoom, MAX_ZOOM_IN, MAX_ZOOM_OUT);
	}
	public float getZoom(){
		return zoom;
	}
	
	
	
}
